/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on a sample project from CommonsWare, LLC
 * From _The Busy Coder's Guide to Advanced Android Development_
 *		http://commonsware.com/AdvAndroid
 */

package edu.bsu.android.gtv.apps.bconnected.ui;

import edu.bsu.android.gtv.apps.bconnected.R;
import edu.bsu.android.gtv.apps.bconnected.objects.Course;
import edu.bsu.android.gtv.apps.bconnected.ui.base.BaseActivity;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;

/**
 * A multi-pane activity, consisting of a {@link TermFragment} and a {@link CourseFragment} 
 * @author Kyle Parker, University Computing Services
 */
public class CourseMultiPaneActivity extends BaseActivity implements 
	TermFragment.OnTermListener,
	CourseFragment.OnCourseListener { 

    private FragmentManager mFragmentManager;
    private TermFragment mTermFragment;
	private Activity mActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

		mActivity = this;
        getActivityHelper().setupActionBar(false, "Course Schedule");
        
        mFragmentManager = getFragmentManager();
        mTermFragment = (TermFragment) mFragmentManager.findFragmentById(R.id.fragment_term_list); 
        mTermFragment.setOnTermListener(this);
        mTermFragment.enablePersistentSelection();
    }

    /**
     * Create a fragment transaction and load the courses for the selected term
     * loadCourses found in {@link CourseFragment}
     * @param position
     */
	public void addCourseFragment(int position) {
		CourseFragment courses = (CourseFragment) mFragmentManager.findFragmentById(R.id.fragment_container_course_list);
		FragmentTransaction ft = mFragmentManager.beginTransaction();
		
		if (courses == null) {
			courses = new CourseFragment(true);
			courses.setOnCourseListener(this);
			
			ft.add(R.id.fragment_container_course_list, courses)
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
				.addToBackStack(null)
				.commit();
		}
			
		courses.startActivity(position);
	}
	
	/**
	 * @see edu.bsu.android.apps.gtv.mybsu.ui.TermFragment.OnTermListener#onTermSelected(int)
	 * When the user taps a term, the OnTermListener in the TermFragment will fire, calling this method
	 * The addCourseFragment method will be called, creating a fragment transaction and loading the courses based on the term selected
	 */
	public void onTermSelected(int position) { 
		addCourseFragment(position);
	}
	
	/**
	 * @see edu.bsu.android.apps.gtv.mybsu.ui.CourseFragment.OnCourseListener#onCourseSelected(edu.bsu.android.apps.gtv.mybsu.objects.Course)
	 * When a course is selected, start a new fragment to display the course information
	 */
	public void onCourseSelected(Course course, int termPosition) {
		Intent intent = new Intent(mActivity, CourseInfoActivity.class);
		intent.putExtra(Constants.EXTRA_COURSE_REF_NUM, course.getCourseRefNum());
        intent.putExtra(Constants.EXTRA_TERM_POSITION, Integer.toString(termPosition));
		
		mActivity.startActivity(intent);
	}
}
